package me.shurufa.widget.recyclerview;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * Author : andy
 * Date   : 16/4/13 09:06
 * Email  : andyxialm@gmail.com
 * Github : github.com/andyxialm
 * <p/>
 * Description : 暂时先针对LinearLayoutManager
 */
public abstract class OnRecyclerViewScrollListener extends EndlessRecyclerOnScrollListener {

    private static final int SCROLL_THRESHOLD_DISTANCE = 30; // 滑动阈值
    private int mScrollDistance;
    private boolean mShow = true;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
        if (firstVisibleItemPosition == 0) {
            return;
        }
        if ((dy > 0 && mShow) || (dy < 0 && !mShow)) {
            mScrollDistance += dy;
        }
        if (mScrollDistance > SCROLL_THRESHOLD_DISTANCE && mShow) {
            executeHideAnim();
            mShow = false;
            mScrollDistance = 0;
        } else if (mScrollDistance < -SCROLL_THRESHOLD_DISTANCE && !mShow) {
            executeShowAnim();
            mShow = true;
            mScrollDistance = 0;
        }
    }

    /**
     * 执行隐藏动画
     */
    public abstract void executeHideAnim();

    /**
     * 执行显示动画
     */
    public abstract void executeShowAnim();
}
